﻿using B.S.Meeting.Domain;
using B.S.Meeting.ErrorCode;
using B.S.Meeting.Infrastructure.Interfaces;
using B.S.Meeting.WriteAPI.Applications.Commands.Roles;
using MediatR;

namespace B.S.Meeting.WriteAPI.Applications.CommandHandlers.Roles
{
    public class DelRoleCommandHandler : IRequestHandler<DelRoleCommand, APIResult<int>>
    {
        IBaseRepository<Role> rRep;
        ILogger<DelRoleCommandHandler> logger;
        public DelRoleCommandHandler(IBaseRepository<Role> rRep, ILogger<DelRoleCommandHandler> logger)
        {
            this.rRep = rRep;
            this.logger = logger;
        }
        public async Task<APIResult<int>> Handle(DelRoleCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var role=await rRep.GetByIdAsync(request.RoleId);
                if (role == null)
                {
                    return new APIResult<int>
                    {
                        Code = StatusEnumCode.Fail,
                        Message = "未找到指定的角色",
                        Data = 0
                    };
                }
                await rRep.Del(request.RoleId); // 直接调用仓储层的删除方法进行物理删除
                logger.LogInformation($"成功删除角色: ID {request.RoleId}");
                return new APIResult<int>
                {
                    Code = StatusEnumCode.Ok,
                    Message = "删除成功",
                    Data = 1
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
